package com.iplant.ams.serviceImpl.dao.dms.maintenance;

import com.iplant.ams.service.basicenum.dms.DMSMaintenanceLevel;
import com.iplant.ams.service.po.dms.maintenance.DMSMaintenanceItemTask;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class DMSMaintenanceItemTaskDAO extends BaseDAO {

    private static final Logger logger = LoggerFactory.getLogger(DMSMaintenanceItemTaskDAO.class);

    private static DMSMaintenanceItemTaskDAO Instance = null;

    private DMSMaintenanceItemTaskDAO() {
        super();
    }

    public static DMSMaintenanceItemTaskDAO getInstance() {
        if (Instance == null)
            Instance = new DMSMaintenanceItemTaskDAO();
        return Instance;
    }

    public List<DMSMaintenanceItemTask> DMS_QueryToDayTaskList(BMSEmployee wLoginUser, int wType,
                                                               OutResult<Integer> wErrorCode) {
        List<DMSMaintenanceItemTask> wResultList = new ArrayList<>();
        try {

            String wSQLText = StringUtils.Format("SELECT t.*,t1.Name CreatorName,t2.Name EditorName,t9.Code " +
                            "DeviceCode,t9" +
                            ".Name DeviceName,t4.Code TaskCode,t5.Name MaintenanceItemName,t6.Name " +
                            "DispatchPersonName,t7.Name" +
                            " WorkerName,t8.Name CheckerName,t5.MaintenanceLevel,t5.SOPList FROM {1}" +
                            ".dms_maintenance_item_task t "
                            + " left join {0}.mbs_user t1 on t.CreatorID=t1.ID "
                            + " left join {0}.mbs_user t2 on t.EditorID=t2.ID "
                            + " left join {1}.dms_device_ledger t3 on t.DeviceID=t3.ID "
                            + " left join {1}.dms_maintenance_task t4 on t.TaskID=t4.ID "
                            + " left join {1}.dms_maintenance_item t5 on t.MaintenanceItemID=t5.ID "
                            + " left join {0}.mbs_user t6 on t.DispatchPersonID=t6.ID "
                            + " left join {0}.mbs_user t7 on t.WorkerID=t7.ID "
                            + " left join {0}.mbs_user t8 on t.CheckerID=t8.ID "
                            + " left join {1}.ams_asset_list t9 on t3.AssetInfoID=t9.ID "
                            + " left join {1}.dms_maintenance_item t11 on t.MaintenanceItemID=t11.ID "
                            + "WHERE (t.Status in(1) or (t.Status=2 and t.EditTime>:StartTime and t"
                            + ".EditTime<:EndTime) ) "
                            + "and ( :wType <=0 or t11.Type = :wType) "
                            + ""
                    , DBSchemas.Basic.getDBName(), DBSchemas.AMS.getDBName());

            Map<String, Object> wParms = new HashMap<>();

            // 今日时间
            Calendar wTodaySTime = Calendar.getInstance();
            wTodaySTime.set(Calendar.HOUR_OF_DAY, 0);
            wTodaySTime.set(Calendar.MINUTE, 0);
            wTodaySTime.set(Calendar.SECOND, 0);
            Calendar wTodayETime = Calendar.getInstance();
            wTodayETime.set(Calendar.HOUR_OF_DAY, 23);
            wTodayETime.set(Calendar.MINUTE, 59);
            wTodayETime.set(Calendar.SECOND, 59);

            wParms.put("StartTime", wTodaySTime);
            wParms.put("EndTime", wTodayETime);
            wParms.put("wType", wType);

            List<Map<String, Object>> wQueryResultList = this.queryForList(wSQLText, wParms);

            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                DMSMaintenanceItemTask wDMSMaintenanceItemTask = new DMSMaintenanceItemTask();
                wDMSMaintenanceItemTask.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wDMSMaintenanceItemTask.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wDMSMaintenanceItemTask.CreatorName = StringUtils.parseString(wSqlDataReader.get("CreatorName"));
                wDMSMaintenanceItemTask.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wDMSMaintenanceItemTask.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wDMSMaintenanceItemTask.EditorName = StringUtils.parseString(wSqlDataReader.get("EditorName"));
                wDMSMaintenanceItemTask.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wDMSMaintenanceItemTask.TaskID = StringUtils.parseInt(wSqlDataReader.get("TaskID"));
                wDMSMaintenanceItemTask.TaskCode = StringUtils.parseString(wSqlDataReader.get("TaskCode"));
                wDMSMaintenanceItemTask.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
                wDMSMaintenanceItemTask.DeviceID = StringUtils.parseInt(wSqlDataReader.get("DeviceID"));
                wDMSMaintenanceItemTask.DeviceCode = StringUtils.parseString(wSqlDataReader.get("DeviceCode"));
                wDMSMaintenanceItemTask.DeviceName = StringUtils.parseString(wSqlDataReader.get("DeviceName"));
                wDMSMaintenanceItemTask.MaintenanceItemID = StringUtils.parseInt(wSqlDataReader.get(
                        "MaintenanceItemID"));
                wDMSMaintenanceItemTask.MaintenanceItemName = StringUtils.parseString(wSqlDataReader.get(
                        "MaintenanceItemName"));
                wDMSMaintenanceItemTask.SOPList = StringUtils.parseList(StringUtils.parseString(wSqlDataReader.get(
                        "SOPList")).split(","));
                wDMSMaintenanceItemTask.DispatchPersonID = StringUtils.parseInt(wSqlDataReader.get("DispatchPersonID"));
                wDMSMaintenanceItemTask.MaintenanceLevel = StringUtils.parseInt(wSqlDataReader.get("MaintenanceLevel"));
                wDMSMaintenanceItemTask.MaintenanceLevelText =
                        DMSMaintenanceLevel.getEnumType(wDMSMaintenanceItemTask.MaintenanceLevel).getLabel();
                wDMSMaintenanceItemTask.DispatchPersonName = StringUtils.parseString(wSqlDataReader.get(
                        "DispatchPersonName"));
                wDMSMaintenanceItemTask.DispatchTime = StringUtils.parseCalendar(wSqlDataReader.get("DispatchTime"));
                wDMSMaintenanceItemTask.PlanDate = StringUtils.parseCalendar(wSqlDataReader.get("PlanDate"));
                wDMSMaintenanceItemTask.DispatchRemark = StringUtils.parseString(wSqlDataReader.get("DispatchRemark"));
                wDMSMaintenanceItemTask.WorkerID = StringUtils.parseInt(wSqlDataReader.get("WorkerID"));
                wDMSMaintenanceItemTask.WorkerName = StringUtils.parseString(wSqlDataReader.get("WorkerName"));
                wDMSMaintenanceItemTask.WorkerTime = StringUtils.parseCalendar(wSqlDataReader.get("WorkerTime"));
                wDMSMaintenanceItemTask.WorkerRemark = StringUtils.parseString(wSqlDataReader.get("WorkerRemark"));
                wDMSMaintenanceItemTask.CheckerID = StringUtils.parseInt(wSqlDataReader.get("CheckerID"));
                wDMSMaintenanceItemTask.CheckerName = StringUtils.parseString(wSqlDataReader.get("CheckerName"));
                wDMSMaintenanceItemTask.CheckerTime = StringUtils.parseCalendar(wSqlDataReader.get("CheckerTime"));
                wDMSMaintenanceItemTask.CheckerRemark = StringUtils.parseString(wSqlDataReader.get("CheckerRemark"));
                wDMSMaintenanceItemTask.Status = StringUtils.parseInt(wSqlDataReader.get("Status"));
                wDMSMaintenanceItemTask.Remark = StringUtils.parseString(wSqlDataReader.get("Remark"));
                wDMSMaintenanceItemTask.Result = StringUtils.parseInt(wSqlDataReader.get("Result"));
                wDMSMaintenanceItemTask.ImageList = StringUtils.parseList(StringUtils.parseString(wSqlDataReader.get(
                        "ImageList")).split(","));

                wResultList.add(wDMSMaintenanceItemTask);
            }
        } catch (Exception ex) {
            logger.error("DMS_QueryToDayTaskList", ex);
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
        }
        return wResultList;
    }

    public List<DMSMaintenanceItemTask> DMS_QueryListByDeviceIDList(BMSEmployee wLoginUser,
                                                                    List<Integer> wDeviceIDList,
                                                                    int wType, OutResult<Integer> wErrorCode) {
        List<DMSMaintenanceItemTask> wResultList = new ArrayList<>();
        try {
            if (wDeviceIDList == null || wDeviceIDList.size() == 0) {
                return wResultList;
            }

            String wSQLText = StringUtils.Format("select t.DeviceID,t.EditTime from {0}.dms_maintenance_item_task t "
                    + " left join {0}.dms_maintenance_item t11 on t.MaintenanceItemID=t11.ID "
                    + "where 1=1 "
                    + "and ( :wType <=0 or t11.Type = :wType) "
                    + "and t.DeviceID" +
                    " in ({1}) and t.Status>=3 ", DBSchemas.AMS.getDBName(), StringUtils.Join(",", wDeviceIDList));

            Map<String, Object> wParms = new HashMap<>();

            wParms.put("wType", wType);

            List<Map<String, Object>> wQueryResultList = this.queryForList(wSQLText, wParms);

            DMSMaintenanceItemTask wDMSMaintenanceItemTask;
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wDMSMaintenanceItemTask = new DMSMaintenanceItemTask();

                wDMSMaintenanceItemTask.DeviceID = StringUtils.parseInt(wSqlDataReader.get("DeviceID"));
                wDMSMaintenanceItemTask.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));

                wResultList.add(wDMSMaintenanceItemTask);
            }
        } catch (Exception ex) {
            logger.error("DMS_QueryListByDeviceIDList", ex);
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
        }
        return wResultList;
    }
}
